package cn.csc.module.member.service.browsinghistory;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.security.core.util.SecurityFrameworkUtils;
import cn.csc.module.member.controller.app.browsinghistory.vo.AppProductBrowsingHistoryCreateReqVO;
import cn.csc.module.member.controller.app.browsinghistory.vo.AppProductBrowsingHistoryExportReqVO;
import cn.csc.module.member.controller.app.browsinghistory.vo.AppProductBrowsingHistoryPageReqVO;
import cn.csc.module.member.controller.app.browsinghistory.vo.AppProductBrowsingHistoryUpdateReqVO;
import cn.csc.module.member.convert.browsinghistory.ProductBrowsingHistoryConvert;
import cn.csc.module.member.dal.dataobject.browsinghistory.ProductBrowsingHistoryDO;
import cn.csc.module.member.dal.mysql.browsinghistory.ProductBrowsingHistoryMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.member.enums.ErrorCodeConstants.PRODUCT_BROWSING_HISTORY_NOT_EXISTS;

/**
 * 商品浏览记录 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class ProductBrowsingHistoryServiceImpl extends ServiceImpl<ProductBrowsingHistoryMapper, ProductBrowsingHistoryDO> implements ProductBrowsingHistoryService {



    @Override
    public BigInteger createOrUpdate(AppProductBrowsingHistoryCreateReqVO createReqVO) {
        ProductBrowsingHistoryDO historyDO = baseMapper.selectByUserIdAndSpuId(BigInteger.valueOf(SecurityFrameworkUtils.getLoginUserId()), createReqVO.getSpuId());
        if (historyDO == null){
            historyDO = ProductBrowsingHistoryConvert.INSTANCE.convert(createReqVO);
            historyDO.setUserId(BigInteger.valueOf(SecurityFrameworkUtils.getLoginUserId()));
            historyDO.setViewTime(LocalDateTime.now());
            historyDO.setViewCount(1);
        } else {
            historyDO.setViewTime(LocalDateTime.now());
            historyDO.setViewCount(historyDO.getViewCount()+1);
        }
        // 插入
        saveOrUpdate(historyDO);
        // 返回
        return historyDO.getId();
    }

    @Override
    public void updateProductBrowsingHistory(AppProductBrowsingHistoryUpdateReqVO updateReqVO) {
        // 校验存在
        validateProductBrowsingHistoryExists(updateReqVO.getId());
        // 更新
        ProductBrowsingHistoryDO updateObj = ProductBrowsingHistoryConvert.INSTANCE.convert(updateReqVO);
        baseMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductBrowsingHistory(BigInteger id) {
        // 校验存在
        validateProductBrowsingHistoryExists(id);
        // 删除
        baseMapper.deleteById(id);
    }

    private void validateProductBrowsingHistoryExists(BigInteger id) {
        if (baseMapper.selectById(id) == null) {
            throw exception(PRODUCT_BROWSING_HISTORY_NOT_EXISTS);
        }
    }

    @Override
    public ProductBrowsingHistoryDO getProductBrowsingHistory(BigInteger id) {
        return baseMapper.selectById(id);
    }


    @Override
    public List<ProductBrowsingHistoryDO> getByUserId(BigInteger userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public List<ProductBrowsingHistoryDO> getProductBrowsingHistoryList(Collection<BigInteger> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProductBrowsingHistoryDO> getProductBrowsingHistoryPage(AppProductBrowsingHistoryPageReqVO pageReqVO) {
        return baseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProductBrowsingHistoryDO> getProductBrowsingHistoryList(AppProductBrowsingHistoryExportReqVO exportReqVO) {
        return baseMapper.selectList(exportReqVO);
    }

    @Override
    public List<ProductBrowsingHistoryDO> getProductBrowsingHistoryList() {
        return baseMapper.getSpuIdList();
    }
}
